package com.hc.pingtan.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hc.pingtan.model.*;
import com.hc.pingtan.service.ISxoOrderService;
import com.hc.pingtan.service.ISxoPluginsCouponService;
import com.hc.pingtan.service.ISxoPluginsCouponUserService;
import com.hc.pingtan.service.ISxoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券 - 应用 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-03
 */
@Api(tags = "优惠卷")
@RestController
@RequestMapping("/sxo-plugins-coupon")
public class SxoPluginsCouponController {

    @Autowired
    private ISxoPluginsCouponService mISxoPluginsCouponService;
    @Autowired
    private ISxoUserService mISxoUserService;
    @Autowired
    private ISxoPluginsCouponUserService mISxoPluginsCouponUserService;

    /**
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "uId", value = "用户ID（可不传）")})
    @ApiOperation(value = "优惠券列表", notes = "优惠券列表", httpMethod = "GET")
    @GetMapping("/findCouponList")
    public CommonResult<List<SxoCouponVo>> findSxoUserOrderVo(String uId) {
        List<SxoCouponVo> lists = Lists.newArrayList();
        //BeanUtil.copyProperties(kekeWitkeyTask, kekeWitkeyTaskDetail);
        QueryWrapper<SxoPluginsCoupon> wrappers = new QueryWrapper();
        wrappers.eq("is_enable", "1");
        wrappers.eq("is_user_receive", "1");
        wrappers.orderByAsc("sort");
        wrappers.orderByDesc("id");
        List<SxoPluginsCoupon> list = mISxoPluginsCouponService.list(wrappers);
        List<SxoPluginsCouponUser> couponUsersData = Lists.newArrayList(); //用户领劵记录
        if (StringUtils.isNotBlank(uId)) {
            //获取用户信息
            QueryWrapper<SxoUser> wrappersUser = new QueryWrapper();
            wrappersUser.eq("username", uId);
            SxoUser one = mISxoUserService.getOne(wrappersUser);
            if (one != null && one.getId() != null) {
                QueryWrapper<SxoPluginsCouponUser> wrappersCouponUser = new QueryWrapper();
                wrappersCouponUser.eq("user_id", one.getId());
                wrappersCouponUser.orderByDesc("is_valid");
                wrappersCouponUser.orderByDesc("is_expire");
                wrappersCouponUser.orderByDesc("is_use");
                couponUsersData = mISxoPluginsCouponUserService.list(wrappersCouponUser);
            }
        }
        Map<Integer,SxoPluginsCouponUser> map= couponUsersData.stream().collect(Collectors.toMap(SxoPluginsCouponUser::getCouponId, Function.identity(), (x, y) -> y));
        for (SxoPluginsCoupon sxoPluginsCoupon : list) {
            SxoCouponVo couponVo = new SxoCouponVo();
            BeanUtil.copyProperties(sxoPluginsCoupon, couponVo);
            SxoPluginsCouponUser couponUser=map.getOrDefault(sxoPluginsCoupon.getId(),null);
            if (couponUser!=null) {
                couponVo.setUserStatus(1);
                couponVo.setIsValid(couponUser.getIsValid()?1:0);
                couponVo.setIsUse(couponUser.getIsUse()?1:0);
            }
            lists.add(couponVo);
        }

        CommonResult<List<SxoCouponVo>> result = CommonResult.<List<SxoCouponVo>>builder().build();
        result.setResult(lists);
        return result;
    }


}
